<template>
  <div>
    <my-header
      ><el-breadcrumb-item>废品管理</el-breadcrumb-item>
      <el-breadcrumb-item>废品价格</el-breadcrumb-item></my-header
    >
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7"
          ><!-- 输入框 -->
          <el-input
            placeholder="请输入废品名称"
            clearable
            v-model="inputValue"
            @clear="getWastePrice"
          >
            <template #append>
              <el-button icon="Search" @click="onSearchList" />
            </template> </el-input
        ></el-col>
      </el-row>

      <!-- 废品价格列表区域 -->
      <el-table :data="WastePriceList" style="width: 100%" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="name" label="废品名称"></el-table-column>
        <el-table-column prop="price" label="价格" sortable></el-table-column>
        <el-table-column prop="remark" label="备注" sortable></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="Edit"
              size="small"
              @click="showEditDialog(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        :page-sizes="[1, 2, 5, 10]"
        small="small"
        background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="WastePriceList.length"
      />
    </el-card>

    <!-- 修改废品价格的对话框 -->
    <el-dialog v-model="editDialogVisible" title="修改废品" width="50%" @close="editDialogClosed">
      <!-- 内容主体区域 -->
      <el-form
        ref="editFormRef"
        :model="editForm"
        status-icon
        :rules="editFormRules"
        label-width="70px"
      >
        <el-form-item label="废品名称" prop="name">
          <el-input v-model="editForm.name" disabled />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="editForm.price" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editWastePrice">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getWaste, updateWaste } from '../../api/waste.js'
export default {
  data() {
    return {
      inputValue: '',
      WastePriceList: [],
      editForm: { name: '', price: '' },
      editDialogVisible: false,
      editFormRules: { price: [{ required: true, message: '请输入废品价格', trigger: 'blur' }] }
    }
  },
  created() {
    this.getWastePrice()
  },
  methods: {
    onSearchList() {
      this.WastePriceList = this.WastePriceList.filter((item) =>
        item.name.includes(this.inputValue)
      )
    },
    // 获取废品价格列表
    async getWastePrice() {
      const { data: res } = await getWaste()
      if (res.code !== 1) {
        return this.$message.error('获取废品价格列表失败')
      }
      this.WastePriceList = res.data
    },
    // 编辑废品价格
    showEditDialog(id) {
      this.editForm = this.WastePriceList.find((item) => item.id === id)
      this.editDialogVisible = true
    },
    // 监听修改废品对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    editWastePrice() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        // 发起修改废品价格的数据请求
        const params = { id: this.editForm.id, price: Number(this.editForm.price) }
        console.log(params)
        const { data: res } = await updateWaste(params)
        if (res.code !== 1) {
          return this.$message.error('更新废品价格失败！')
        }
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getWastePrice()
        // 提示修改成功
        this.$message.success('更新废品价格成功！')
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
